package test;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.CheckboxMenuItem;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.tools.Tool;

import org.omg.CORBA.PUBLIC_MEMBER;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.KeyAdapter;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Demo extends JFrame {
	
	private JPanel contentPane;
	public static JTable table;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	final TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit()
			.getImage(Demo.class.getResource("/test/images/x.png")));
	private JLabel lblNewLabel;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Demo frame = new Demo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	// new ImageIcon(Demo.class.getResource("/test/images/1.gif")
	public Demo() {
		setTitle("Main frame");
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				Demo.class.getResource("/test/images/check.png")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 455);
		setTray();
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		JMenu fileMenu = new JMenu("File");
		menuBar.add(fileMenu);

		JMenuItem exitMenuItem = new JMenuItem("Exit");
		exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4,
				InputEvent.ALT_MASK));
		fileMenu.add(exitMenuItem);

		JMenu aboutMenuItem = new JMenu("Help");
		menuBar.add(aboutMenuItem);

		JMenuItem mntmAbout = new JMenuItem("About");
		aboutMenuItem.add(mntmAbout);
		contentPane = new JPanel();
		setContentPane(contentPane);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(15, 16, 252, 135);

		JButton btnOk = new JButton("OK");

		btnOk.setBounds(15, 157, 70, 23);
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});

		JButton btnEdit = new JButton("Edit");
		btnEdit.setBounds(273, 34, 53, 23);
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				About t = new About();
				t.setVisible(true);
				// table.setValueAt(t.textField_1.getText(), 0, 0);
				// table.setValueAt(t.textField.getText(), 0, 1);

			}
		});

		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(332, 157, 32, 32);

		table = new JTable();
		table.setModel(new DefaultTableModel(new Object[][] { { "0851120036",
				"Bùi Đức Kiên" }, }, new String[] { "MASSV", "HOTEN" }) {
			Class[] columnTypes = new Class[] { String.class, String.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		scrollPane.setViewportView(table);
		contentPane.setLayout(null);
		contentPane.add(panel);
		contentPane.add(scrollPane);
		contentPane.add(btnEdit);
		contentPane.add(btnOk);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.setBounds(364, 157, 32, 32);
		contentPane.add(panel_1);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_2.setBounds(299, 157, 32, 32);
		contentPane.add(panel_2);

		JButton button = new JButton("");
		button.setBackground(Color.WHITE);
		button.setIcon(new ImageIcon(Demo.class
				.getResource("/test/images/check.png")));
		button.setBounds(15, 232, 32, 32);
		contentPane.add(button);

		JButton button_1 = new JButton("");
		button_1.setBackground(Color.WHITE);
		button_1.setIcon(new ImageIcon(Demo.class
				.getResource("/test/images/x.png")));
		button_1.setBounds(47, 232, 32, 32);
		contentPane.add(button_1);

		JButton button_2 = new JButton("");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				trayIcon.displayMessage("HI", "JO", MessageType.ERROR);
			}
		});
		button_2.setBackground(Color.WHITE);
		button_2.setBounds(79, 232, 32, 32);
		contentPane.add(button_2);

		JButton button_3 = new JButton("");
		button_3.setBackground(Color.WHITE);
		button_3.setBounds(111, 232, 32, 32);
		contentPane.add(button_3);

		JButton button_4 = new JButton("");
		button_4.setBounds(143, 232, 32, 32);
		contentPane.add(button_4);

		JButton button_5 = new JButton("");
		button_5.setBounds(143, 264, 32, 32);
		contentPane.add(button_5);

		JButton button_6 = new JButton("");
		button_6.setBounds(111, 264, 32, 32);
		contentPane.add(button_6);

		JButton button_7 = new JButton("");
		button_7.setBounds(79, 264, 32, 32);
		contentPane.add(button_7);

		JButton button_8 = new JButton("");
		button_8.setBounds(47, 264, 32, 32);
		contentPane.add(button_8);

		JButton button_9 = new JButton("");
		button_9.setBounds(15, 264, 32, 32);
		contentPane.add(button_9);

		JButton btnNewButton = new JButton("Ẩn");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				an();
			}
		});
		btnNewButton.setBounds(111, 178, 89, 23);
		contentPane.add(btnNewButton);

		JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
		buttonGroup.add(rdbtnNewRadioButton);
		rdbtnNewRadioButton.setBounds(220, 208, 109, 23);
		contentPane.add(rdbtnNewRadioButton);

		JRadioButton rdbtnNewRadioButton_1 = new JRadioButton(
				"New radio button");
		buttonGroup.add(rdbtnNewRadioButton_1);
		rdbtnNewRadioButton_1.setBounds(220, 232, 109, 23);
		contentPane.add(rdbtnNewRadioButton_1);
		
		lblNewLabel = new JLabel("New label");
		lblNewLabel.setBounds(220, 264, 46, 14);
		contentPane.add(lblNewLabel);
		
		textField = new JTextField();
		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				tinh();
			}
		});
		textField.setBounds(25, 329, 86, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(25, 360, 86, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(124, 329, 86, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setBounds(124, 360, 86, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		JButton btnTinh = new JButton("Tinh");
		btnTinh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tinh();
			}
		});
		btnTinh.setBounds(235, 328, 91, 23);
		contentPane.add(btnTinh);
	}

	public void btnOkActionPerformed() {
		this.table.setValueAt("Cái lồn", 0, 1);
	}

	public void an() {
		this.setVisible(false);
		trayIcon.displayMessage("Thông báo", "Chương trình đã được ẩn",
				MessageType.INFO);
	}

	public void setTray(){
		//Check the SystemTray is supported
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final PopupMenu popup = new PopupMenu();
        
        //Toolkit.getDefaultToolkit().createImage(Demo.class.getResource("/test/images/check.png").toString());
        final SystemTray tray = SystemTray.getSystemTray();
       
        // Create a popup menu components
        MenuItem aboutItem = new MenuItem("About");
        MenuItem showItem = new MenuItem("Show");
        CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
        CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
        Menu displayMenu = new Menu("Display");
        MenuItem errorItem = new MenuItem("Error");
        MenuItem warningItem = new MenuItem("Warning");
        MenuItem infoItem = new MenuItem("Info");
        MenuItem noneItem = new MenuItem("None");
        MenuItem exitItem = new MenuItem("Exit");
        
        //Add components to popup menu
        popup.add(aboutItem);
        popup.add(showItem);
        popup.addSeparator();
        popup.add(cb1);
        popup.add(cb2);
        popup.addSeparator();
        popup.add(displayMenu);
        displayMenu.add(errorItem);
        displayMenu.add(warningItem);
        displayMenu.add(infoItem);
        displayMenu.add(noneItem);
        popup.add(exitItem);
        
        trayIcon.setPopupMenu(popup);
        
        try {
            tray.add(trayIcon);
        } catch (Exception e) {
            System.out.println("TrayIcon could not be added.");
        }
	}

	public void showWindows() {
		this.setVisible(true);

	}
	public void trayIconMouseClicked(MouseEvent e){
		if(e.getClickCount()==2){
			showWindows();
			this.setExtendedState(JFrame.NORMAL);
		}
	}
	public void tinh(){
		int value = Integer.valueOf(textField.getText());
		int i=value;
		StringBuffer binary = new StringBuffer();
		while(i!=0){
			binary.insert(0, i%2);
			i= i/2;
		}
		textField_1.setText(binary.toString());
		
		textField_2.setText(String.valueOf(1&0));
		
		int value1 = Integer.valueOf(textField_2.getText());
		int j=value1;
		StringBuffer binary1 = new StringBuffer();
		while(j!=0){
			binary1.insert(0, j%2);
			j= j/2;
		}
		textField_3.setText(binary1.toString());
	}
}
